package cn.schoolwow.quickdao.test.module.business.user.dql.subquery.flow;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.test.entity.Role;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

public class AddExistSubQueryFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = (User) flowContext.checkData("insertUser");
        Role insertRole = (Role) flowContext.checkData("insertRole");

        Condition roleCondition = dao.query(Role.class)
                .addQuery("roleKey", insertRole.getRoleKey())
                .addQuery("roleName", insertRole.getRoleName())
                .addQuery("status", insertRole.getStatus())
                .addColumn("id")
                .limit(0,1);

        String username = dao.query(User.class)
                .addExistSubQuery(roleCondition)
                .addColumn("username")
                .execute()
                .getSingleColumn(String.class);
        Assert.assertEquals(insertUser.getUsername(), username);
    }

    @Override
    public String name() {
        return "添加是否存在子查询";
    }
}